package com.gxw.rxjavatest.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AndroidException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.gxw.rxjavatest.Presenter.UserPresenter;
import com.gxw.rxjavatest.R;
import com.gxw.rxjavatest.activity.View.UserView;
import com.gxw.rxjavatest.model.bean.User;
import com.gxw.rxjavatest.utils.MyToast;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity implements UserView {

    private String TAG = MainActivity.class.getName().toString();

    @ViewInject(R.id.btn_to_sec)
    private Button btn_to_sec;

    @ViewInject(R.id.tv_page_name)
    private TextView tv_page_name;

    private ProgressDialog mProgressDialog;
    private UserPresenter userPresenter;

//    private Observable<String> observable;

    private String name[] = {"123", "456", "789"};

    private Subscriber<String> subscriber = new Subscriber<String>() {

        @Override
        public void onCompleted() {
            Log.d(TAG, "onCompleted");
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
            Log.d(TAG, "onError");
        }

        @Override
        public void onNext(String t) {
            Log.d(TAG, "onNext:" + t);
//            mHello.setText(t);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //必要
        x.view().inject(this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        userPresenter = new UserPresenter(this);
        tv_page_name.setText("你好，世界");
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("正在加载");

    }

    /**
     * 1. 方法必须私有限定,
     * 2. 方法参数形式必须和type对应的Listener接口一致.
     * 3. 注解参数value支持数组: value={id1, id2, id3}
     * 4. 其它参数说明见{@link Event}类的说明.
     **/
    @Event(value = {R.id.btn_to_sec, R.id.btn_to_third, R.id.fab, R.id.btn_main_f}, type = View.OnClickListener.class)
    private void onClicked(View v) {

        switch (v.getId()) {
            case R.id.btn_main_f:
                Log.i(TAG, " -------------------- btn_main_f ------------------- ");
//                observable.just(name);
                Observable<String> stringObservable = Observable.create(new Observable.OnSubscribe<String>() {
                    @Override
                    public void call(Subscriber<? super String> subscriber) {

                        String s = "ni hao";
                        subscriber.onNext(s);
                        subscriber.onCompleted();

                    }
                });
                stringObservable.subscribeOn(Schedulers.io())//在非UI线程中执行getUser
                        .observeOn(AndroidSchedulers.mainThread())//在UI线程中执行结果
                        .subscribe(subscriber);
//                subscriber.onCompleted();


                break;
            case R.id.btn_to_sec:

                MyToast.initToast(MainActivity.this, "btn_to_sec");
//                observable.just(name);

                startActivity(new Intent(MainActivity.this, SecondActivity.class));

//                Observable.from(name).subscribe(new Action1<String>() {
//                    @Override
//                    public void call(String s) {
//                        Log.i(TAG, s);
//                    }
//                });

                break;
            case R.id.btn_to_third:

                Log.i("sequence", "-------------- btn_to_third --------------");
                userPresenter.getUser();

                break;
            case R.id.fab:

                MyToast.initToast(MainActivity.this, "fab");

//                Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();

                break;
        }

    }

    Observable observable = Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {

//            subscriber.onNext("hello");
//            subscriber.onNext("gxw");
//            subscriber.onNext("welcome to a new world");
//            subscriber.onCompleted();

        }
    });

    Action1<String> onNextAction = new Action1<String>() {
        @Override
        public void call(String s) {

            Log.i(TAG, s);

        }
    };

    @Override
    public void updateView(User user) {
        if (user == null) return;
        Log.i("sequence", "-------------- updateView --------------");
        tv_page_name.setText(user.getName());
    }

    @Override
    public void showProgressDialog() {
        Log.i("sequence", "-------------- showProgressDialog --------------");
        mProgressDialog.show();
    }

    @Override
    public void hideProgressDialog() {
        Log.i("sequence", "-------------- hideProgressDialog --------------");
        mProgressDialog.hide();
    }

    @Override
    public void showError(String msg) {
        MyToast.initToast(this, msg);
    }
}
